package net.thelostlands.plugins.thelostfortress;

import com.palmergames.bukkit.towny.Towny;
import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Logger;
import net.milkbowl.vault.Vault;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author fabio
 */
public class Main extends JavaPlugin {
    
    
    public static final Logger log = Logger.getLogger("Minecraft");
    CommandHandler cmdExec;
    public static Main one;
    public static Permission permissions;
    public static boolean guerraAttiva;
    public static boolean construction;
    public static Towny towny;
    public static AreaSet area;
    public static String dataFile;
    public static String mondo = "world";
    public static int maxChest;
    HashSet<Material> unbreakable = null, placeable = null;
    Vault vault;

    @Override
    public void onDisable() {
        log.info("[TheLostFortress] Exit.");
    }

    @Override
    public void onEnable() {

        one=this;
        dataFile = "plugins/TheLostFortress/";
        File dir = new File(dataFile);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        vault = (Vault) this.getServer().getPluginManager().getPlugin("Vault");
        if (vault == null) {
            log.severe("[TheLostFortress] Vault Not Found. Exit.");
            return;
        }
        permissions = this.getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class).getProvider();
        
        towny = (Towny) getServer().getPluginManager().getPlugin("Towny");
        log.info(String.format("[%s] Using %s", getDescription().getName(), permissions.getName()));
        if (towny == null || permissions == null) {
            log.severe("[TheLostFortress] Plugin necessari non trovati. Esco");
            return;
        }
        cmdExec = new CommandHandler();
        getCommand("fortress").setExecutor(cmdExec);
        PluginManager pm = getServer().getPluginManager();
        ListenToEntity le = new ListenToEntity();
        unbreakable = new HashSet<Material>();
        placeable = new HashSet<Material>();
        loadMaterials();
        ListenToBlock lb = new ListenToBlock(unbreakable, placeable);
        ListenToPlayer lp = new ListenToPlayer();
        
//        pm.registerEvent(Type.BLOCK_BREAK, lb, Priority.Low, this);
//        pm.registerEvent(Type.BLOCK_PLACE, lb, Priority.Low, this);
//        pm.registerEvent(Type.BLOCK_BURN, lb, Priority.Low, this);
//        pm.registerEvent(Type.ENTITY_EXPLODE, le, Priority.Low, this);
//        pm.registerEvent(Type.ENTITY_DAMAGE, le, Priority.Low, this);
//        pm.registerEvent(Type.PLAYER_MOVE, lp, Priority.Low, this);
//        pm.registerEvent(Type.PLAYER_INTERACT, lp, Priority.Low, this);
                getServer().getPluginManager().registerEvents(lb, this);
                getServer().getPluginManager().registerEvents(lp, this);
                getServer().getPluginManager().registerEvents(le, this);

        area = new AreaSet();
        area.load();


    }
    
    private void loadMaterials(){
        FileConfiguration config = getConfig();
        List<Integer> breaks= config.getIntegerList("unbreak");
        List<Integer> places = config.getIntegerList("place");
        String world = config.getString("world");
        Main.mondo=world;
        for(Integer s : breaks){
            unbreakable.add(Material.getMaterial(s));
            log.info("[TheLostFortress] You cannot brake: "+Material.getMaterial(s));
        }
        for(Integer s : places){
            placeable.add(Material.getMaterial(s));
            log.info("[TheLostFortress] You can place: "+Material.getMaterial(s));
        }
        maxChest = config.getInt("chest", 4);
        
    }

    public void reloadFortressFromFile(){
        area.clear();
        area.load();
    }

}
